package com.sq.subject.application.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.google.common.base.Preconditions;
import com.sq.subject.application.dto.*;
import com.sq.subject.common.entity.Result;
import com.sq.subject.common.enums.SubjectInfoTypeEnum;
import com.sq.subject.domain.bo.*;
import com.sq.subject.domain.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author quan
 */
@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    @PostMapping("/add")
    public Result<Boolean> addSubject(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectController.addSubject.dto：{}", JSON.toJSONString(subjectInfoDTO));
        }
        try {
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分值不能为空");
            Preconditions.checkArgument(!StrUtil.isBlank(subjectInfoDTO.getSubjectParse()), "题目解析不能为空");
            Preconditions.checkArgument(!CollUtil.isEmpty(subjectInfoDTO.getCategoryIds()), "题目分类不能为空");
            Preconditions.checkArgument(!CollUtil.isEmpty(subjectInfoDTO.getLabelIds()), "题目标签不能为空");
            if(!subjectInfoDTO.getSubjectType().equals(SubjectInfoTypeEnum.SIMPLE.getCode())){
                Preconditions.checkArgument(!CollUtil.isEmpty(subjectInfoDTO.getOptionList()), "题目选项不能为空");
            }
            SubjectInfoBO subjectInfoBO = BeanUtil.copyProperties(subjectInfoDTO, SubjectInfoBO.class);
            List<SubjectAnswerBO> subjectAnswerBOList = BeanUtil.copyToList(subjectInfoDTO.getOptionList(), SubjectAnswerBO.class);
            subjectInfoBO.setOptionList(subjectAnswerBOList);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectController.addSubject.error:{}", e.getMessage(), e);
            return Result.fail("新增题目失败");
        }
    }

    @PostMapping("/getSubjectPage")
    public Result<SubjectPageResultDTO> getSubjectPage(@RequestBody SubjectPageQueryDTO subjectPageQueryDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectController.getSubjectPage.dto：{}", JSON.toJSONString(subjectPageQueryDTO));
        }
        try {
            Preconditions.checkNotNull(subjectPageQueryDTO.getCategoryId(), "题目分类不能为空");
            Preconditions.checkNotNull(subjectPageQueryDTO.getLabelId(), "题目标签不能为空");
            Preconditions.checkNotNull(subjectPageQueryDTO.getSubjectDifficult(), "题目难度不能为空");
            SubjectPageQueryBO subjectPageQueryBO =  BeanUtil.copyProperties(subjectPageQueryDTO, SubjectPageQueryBO.class);
            SubjectPageResultBO subjectPageResultBO = subjectInfoDomainService.getSubjectPage(subjectPageQueryBO);
            SubjectPageResultDTO subjectPageResultDTO = BeanUtil.copyProperties(subjectPageResultBO, SubjectPageResultDTO.class);
            subjectPageResultDTO.setResult(BeanUtil.copyToList(subjectPageResultBO.getResult(), SubjectPageDTO.class));
            return Result.ok(subjectPageResultDTO);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目列表失败");
        }
    }

    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectController.querySubjectInfo.dto：{}", JSON.toJSONString(subjectInfoDTO));
        }
        try {
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            SubjectInfoBO subjectInfoBO =   BeanUtil.copyProperties(subjectInfoDTO, SubjectInfoBO.class);
            SubjectInfoBO subjectResultBO = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO infoDTO = BeanUtil.copyProperties(subjectResultBO, SubjectInfoDTO.class);
            List<SubjectAnswerBO> optionList = subjectResultBO.getOptionList();
            List<SubjectAnswerDTO> subjectAnswerDTOList = BeanUtil.copyToList(optionList, SubjectAnswerDTO.class);
            infoDTO.setOptionList(subjectAnswerDTOList);
            return Result.ok(infoDTO);
        } catch (Exception e) {
            log.error("SubjectController.querySubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

}
